/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.WebIDE.ejb.sessions;

import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
import org.WebIDE.ejb.entities.*;
import org.WebIDE.ejb.sessions.exceptions.*;

/**
 *
 * @author huy
 */
@Stateless
public class GroupsResourcesFacade implements GroupsResourcesFacadeLocal, GroupsResourcesFacadeRemote {
    @PersistenceContext
    private EntityManager _em;
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "WebIDE-ejbPU")
    private EntityManagerFactory emf = null;
/*
    public void create(GroupsResources groupsResources) {
        _em.persist(groupsResources);
    }

    public void edit(GroupsResources groupsResources) {
        _em.merge(groupsResources);
    }
*/
    public void remove(GroupsResources groupsResources) {
        _em.remove(_em.merge(groupsResources));
    }

    public GroupsResources find(Object id) {
        return _em.find(GroupsResources.class, id);
    }

    public List<GroupsResources> findAll() {
        return _em.createQuery("select object(o) from GroupsResources as o").getResultList();
    }


    public EntityManager getEntityManager() {
        return _em;
    }

    public void create(GroupsResources groupsResources) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (groupsResources.getGroupsResourcesPK() == null) {
            groupsResources.setGroupsResourcesPK(new GroupsResourcesPK());
        }
        groupsResources.getGroupsResourcesPK().setResourceId(groupsResources.getResources().getId());
        groupsResources.getGroupsResourcesPK().setGroupName(groupsResources.getGroups().getGroupName());
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            Groups groups = groupsResources.getGroups();
            if (groups != null) {
                groups = em.getReference(groups.getClass(), groups.getGroupName());
                groupsResources.setGroups(groups);
            }
            Resources resources = groupsResources.getResources();
            if (resources != null) {
                resources = em.getReference(resources.getClass(), resources.getId());
                groupsResources.setResources(resources);
            }
            em.persist(groupsResources);
            if (groups != null) {
                groups.getGroupsResourcesCollection().add(groupsResources);
                groups = em.merge(groups);
            }
            if (resources != null) {
                resources.getGroupsResourcesCollection().add(groupsResources);
                resources = em.merge(resources);
            }
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findGroupsResources(groupsResources.getGroupsResourcesPK()) != null) {
                throw new PreexistingEntityException("GroupsResources " + groupsResources + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public void edit(GroupsResources groupsResources) throws NonexistentEntityException, RollbackFailureException, Exception {
        groupsResources.getGroupsResourcesPK().setResourceId(groupsResources.getResources().getId());
        groupsResources.getGroupsResourcesPK().setGroupName(groupsResources.getGroups().getGroupName());
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            GroupsResources persistentGroupsResources = em.find(GroupsResources.class, groupsResources.getGroupsResourcesPK());
            Groups groupsOld = persistentGroupsResources.getGroups();
            Groups groupsNew = groupsResources.getGroups();
            Resources resourcesOld = persistentGroupsResources.getResources();
            Resources resourcesNew = groupsResources.getResources();
            if (groupsNew != null) {
                groupsNew = em.getReference(groupsNew.getClass(), groupsNew.getGroupName());
                groupsResources.setGroups(groupsNew);
            }
            if (resourcesNew != null) {
                resourcesNew = em.getReference(resourcesNew.getClass(), resourcesNew.getId());
                groupsResources.setResources(resourcesNew);
            }
            groupsResources = em.merge(groupsResources);
            if (groupsOld != null && !groupsOld.equals(groupsNew)) {
                groupsOld.getGroupsResourcesCollection().remove(groupsResources);
                groupsOld = em.merge(groupsOld);
            }
            if (groupsNew != null && !groupsNew.equals(groupsOld)) {
                groupsNew.getGroupsResourcesCollection().add(groupsResources);
                groupsNew = em.merge(groupsNew);
            }
            if (resourcesOld != null && !resourcesOld.equals(resourcesNew)) {
                resourcesOld.getGroupsResourcesCollection().remove(groupsResources);
                resourcesOld = em.merge(resourcesOld);
            }
            if (resourcesNew != null && !resourcesNew.equals(resourcesOld)) {
                resourcesNew.getGroupsResourcesCollection().add(groupsResources);
                resourcesNew = em.merge(resourcesNew);
            }
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                GroupsResourcesPK id = groupsResources.getGroupsResourcesPK();
                if (findGroupsResources(id) == null) {
                    throw new NonexistentEntityException("The groupsResources with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public void destroy(GroupsResourcesPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            GroupsResources groupsResources;
            try {
                groupsResources = em.getReference(GroupsResources.class, id);
                groupsResources.getGroupsResourcesPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The groupsResources with id " + id + " no longer exists.", enfe);
            }
            Groups groups = groupsResources.getGroups();
            if (groups != null) {
                groups.getGroupsResourcesCollection().remove(groupsResources);
                groups = em.merge(groups);
            }
            Resources resources = groupsResources.getResources();
            if (resources != null) {
                resources.getGroupsResourcesCollection().remove(groupsResources);
                resources = em.merge(resources);
            }
            em.remove(groupsResources);
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public List<GroupsResources> findGroupsResourcesEntities() {
        return findGroupsResourcesEntities(true, -1, -1);
    }

    public List<GroupsResources> findGroupsResourcesEntities(int maxResults, int firstResult) {
        return findGroupsResourcesEntities(false, maxResults, firstResult);
    }

    private List<GroupsResources> findGroupsResourcesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from GroupsResources as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            //em.close();
        }
    }

    public GroupsResources findGroupsResources(GroupsResourcesPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(GroupsResources.class, id);
        } finally {
            //em.close();
        }
    }

    public int getGroupsResourcesCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from GroupsResources as o").getSingleResult()).intValue();
        } finally {
            //em.close();
        }
    }

}
